let MyGameObjects = [];

class MyGameObject {

    constructor() {
        MyGameObjects.push(this);
        this.hasCalledStart = false; // 是否执行过start函数
        this.timeDelta = 0; // 当前帧距离上一帧的时间间隔
    }

    start() { // 只会在第一帧执行一次

    }

    update() { // 每一帧执行一次

    }

    on_destroy() { // 在被销毁前执行一次

    }

    destroy() { // 删掉该物体
        this.on_destroy();

        for (let i = 0; i < MyGameObjects.length; i++) {
            if (MyGameObjects[i] === this) {
                MyGameObjects.splice(i, 1);
                break;
            }
        }
    }
}

let lastTimestamp;
let MyGameAnimation = function (timestamp) {
    for (let i = 0; i < MyGameObjects.length; i++) {
        let obj = MyGameObjects[i];
        if (!obj.hasCalledStart) {
            obj.start();
            obj.hasCalledStart = true;
        } else {
            obj.timeDelta = timestamp - lastTimestamp;
            obj.update();
        }
    }
    lastTimestamp = timestamp;

    requestAnimationFrame(MyGameAnimation);
}

requestAnimationFrame(MyGameAnimation);